/**
 * 预约建档初始化
 */
var RevisitStatisticsTable = {
    id: "revisitStatisticsTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};
function _formatter(value, row, index) {
    if (row.status === "到店") {
        return '<span style="color: green" title="' + value + '">' + value + '</span>'
    } else {
        return '<span title="' + value + '">' + value + '</span>'
    }
}
function _formatterTime(value, row, index) {
    if (row.status === "到店") {
        return "<span style='color: green'>" + value.substring(0, 19) + "</span>"
    } else {
        return value.substring(0, 19);
    }
}
/**
 * 初始化表格的列
 */
RevisitStatisticsTable.initColumn = function () {
    var columns = [
        {field: 'selectItem', radio: true},
        {title: '', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: '', field: 'customerSexHead ', visible: false, align: 'center', valign: 'middle'},
        {title: '预约号', field: 'reservationsNum', align: 'center', valign: 'middle', sortable: true, width: 110,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        },
        {title: '姓名', field: 'customerName', align: 'center', valign: 'middle', sortable: true, width: 120,
            formatter: function (value, row, index) {
                if (row.status === "到店") {
                    return "<a href='javascript:;' style='color: green;display: flex;align-items: center;' ><img src='" + row.customerSexHead + "' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                } else {
                    return "<a href='javascript:;' style='display: flex;align-items: center;' ><img src='" + row.customerSexHead + "' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                }
            }
        },
        {title: '手机号码', field: 'customerPhone', align: 'center', valign: 'middle', sortable: true, width: 140,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        },
        {title: '预约门诊', field: 'outpatientName', align: 'center', valign: 'middle', sortable: true, width: 110,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        },
        {title: '到店状态', field: 'status', align: 'center', valign: 'middle', sortable: true, width: 110,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        },
        {title: '回访状态', field: 'statuss', align: 'center', valign: 'middle', sortable: true, width: 110,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        },
        {title: '回访人', field: 'revisit', align: 'center', valign: 'middle',width: 90,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        },
        {title: '回访时间', field: 'revisitTime', align: 'center', valign: 'middle', sortable: true, width: 170,
            formatter: function (value, row, index) {
                return _formatterTime(value, row, index);
            }
        },
        {title: '回访内容', field: 'revisitContent', align: 'center', valign: 'middle',width: 170,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            },
            cellStyle: function (value, row, index) {
                return {css: {"overflow":"hidden","white-space":"nowrap","text-overflow":"ellipsis"}}
            }
        },
        {title: '预约时间', field: 'appointmentTime', align: 'center', valign: 'middle', sortable: true, width: 170,
            formatter: function (value, row, index) {
                return _formatterTime(value, row, index);
            }
        },
        {title: '初诊时间', field: 'firstVisitDate', align: 'center', valign: 'middle', sortable: true, width: 170,
            formatter: function (value, row, index) {
                return _formatterTime(value, row, index);
            }
        },
        {title: '建档人', field: 'archiving', align: 'center', valign: 'middle', sortable: true, width: 90,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        },
        {title: '跟进人', field: 'follower', align: 'center', valign: 'middle', sortable: true, width: 90,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        },
        {title: '归属人', field: 'belonger', align: 'center', valign: 'middle', sortable: true, width: 90,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        },
        {title: '部门', field: 'department', align: 'center', valign: 'middle', sortable: true, width: 100,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        },
        {title: '建档时间', field: 'filingTime', align: 'center', valign: 'middle', sortable: true, width: 170,
            formatter: function (value, row, index) {
                return _formatterTime(value, row, index);
            }
        },
        {title: '一级渠道', field: 'oneChannel', align: 'center', valign: 'middle', sortable: true, width: 110,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        },
        {title: '二级渠道', field: 'twoChannel', align: 'center', valign: 'middle', sortable: true, width: 110,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        },
        {title: '三级渠道', field: 'thrChannel', align: 'center', valign: 'middle', sortable: true, width: 130,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        },
        {title: '性别', field: 'sex', align: 'center', valign: 'middle', sortable: true, width: 80,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        },
        {title: '年龄', field: 'customerAge', align: 'center', valign: 'middle', sortable: true, width: 80,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        },
        {title: '开单状态',visible: false,field: 'dealStatus',align: 'center',valign: 'middle',sortable: true,width: 100,
            formatter: function (value, row, index) {
                if(row.status === "到店"){
                    if (value === "已结账") {
                        return "<a href='javascript:;' style='color: green' onclick='ReservationsRecordTable.opendealStatus(" + '"' + row.id + '"' + ")'>[已结账]</a>"
                    } else {
                        return "<span style='color: green'>" + value + "</span>"
                    }
                }else {
                    return "<span>" + value + "</span>"
                }
            }
        },
        {title: '访问病种', field: 'accessDisease', align: 'center', valign: 'middle', sortable: true, width: 110,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        },
        {title: '预约病种',field: 'appointmentPayService',align: 'center',valign: 'middle',sortable: true,width: 110,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        }
    ];
    return columns;
};
/**
 * 查询预约客户列表
 */
RevisitStatisticsTable.search = function () {
    RevisitStatisticsTable.table.refresh({query: RevisitStatisticsTable.formParams()});
    // RevisitStatisticsTable.statisticTongJi();
};
/**
 * 统计建档，已到，未到 人数
 */
// RevisitStatisticsTable.statisticTongJi = function () {
//     var param1 = multipleSelection($('#outpatientName').val());
//     var param2 = $("#accessDisease").val();   //访问病种
//     var param3 = $("#appointmentPayService").val();   //预约病种
//     var param4 = $("#follower").val(); //回访人
//     var param5 = $("#startFilingTime").val();  //建档时间
//     var param6 = $("#endFilingTime").val();  //
//     var date = new Date();
//     var currentWeek = date.getDay();
//     var stepSunDay = -currentWeek + 1;
//     if (currentWeek == 0) {
//         stepSunDay = -7;
//     }
//     var stepMonday = 7 - currentWeek;
//     var time = date.getTime();
//     var monday = new Date(time + stepSunDay * 24 * 3600 * 1000);
//     var sunday = new Date(time + stepMonday * 24 * 3600 * 1000);
//     var param7 = setDateTime(monday,0) + " " + "00:00:00";
//     var param8 = setDateTime(sunday,0) + " " + "23:59:59";
//     var param9 =  multipleSelection($('#area').val());
//     var param10 = $("#startRevisitTime").val();  //建档时间
//     var param11 = $("#endRevisitTime").val();  //
//     var param12 =  $("#marketInputPublic", parent.document).val();  //姓名   电话   微信  预约号
//     $.ajax({
//         type: "GET",
//         url: Feng.ctxPath + "/returnVisitStatistics/tongji",
//         data: {
//             param1: param1, param2: param2, param3: param3,
//             param4: param4, param5: param5, param6: param6,
//             param7: param7, param8: param8, param9: param9,
//             param10: param10, param11: param11,param12:param12
//         },
//         dataType: "json",
//         success: function (data) {
//             $("#huifangToday").html(data[0].countNum);//今日
//             $("#huifangYesterday").html(data[1].countNum);//昨日
//             $("#huifangThisWeek").html(data[2].countNum);//本周
//             $("#huifangMonth").html(data[3].countNum);//本月
//             $("#huifangLastMonth").html(data[4].countNum);//同比
//         }
//     });
// };
/**
 * 把对象拼接成URL的键值对参数
 * param1=value1&param2=value2
 */
var parseParam = function (param) {
    var paramStr = "";
    $.each(param, function (i) {
        paramStr += '&' + getSingleData(param, i);
    });
    return paramStr.substr(1);
};
var getSingleData = function (param, key) {
    return key + "=" + param[key];
};
/**
 *  新 市场预约表单查询提交参数
 * @returns {{}}
 */
RevisitStatisticsTable.formParams = function () {
    var queryData = {};
    queryData['reach'] = $("#marketInputPublic", parent.document).val();
    queryData["outpatientName"] = multipleSelection($('#outpatientName').val());
    queryData['accessDisease'] = $("#accessDisease").val();  //访问病种
    queryData['appointmentPayService'] = $("#appointmentPayService").val();
    queryData['startFilingTime'] = $("#startFilingTime").val();  //建档时间
    queryData['endFilingTime'] = $("#endFilingTime").val();
    queryData['startRevisitTime'] = $("#startRevisitTime").val();  //回访时间
    queryData['endRevisitTime'] = $("#endRevisitTime").val();
    queryData['revisit'] = $("#follower").val();     //回访人
    queryData['area'] = multipleSelection($('#area').val());
    return queryData;
};
/**
 * 片区筛选
 */
RevisitStatisticsTable.getData = function () {
    var area = multipleSelection($('#area').val());
    $.post(Feng.ctxPath + "/dept/getListByArea/"+area,function (data) {
        $('#outpatientName').empty();
        for (var i = 0; i < data.length; i++) {
            $('#outpatientName').append("<option value=" + data[i].simplename + ">" + data[i].simplename + "</option>");
        }
        $('#outpatientName').selectpicker('refresh');
        $('#outpatientName').selectpicker('render');
        RevisitStatisticsTable.search();
    }, "json");
};

RevisitStatisticsTable.getArchiving = function () { // 选择建档人
    var index = layer.open({
        type: 2,
        title: '选择建档人',
        area: ['70%', '60%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/reservation/to_archiving'
    });
    this.layerIndex = index;
};

RevisitStatisticsTable.getFollower = function () { // 添加跟进人
    var index = layer.open({
        type: 2,
        title: '添加跟进人',
        area: ['70%', '60%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/reservations/to_follower'
    });
    this.layerIndex = index;
};

RevisitStatisticsTable.downLoad = function () { // 导出
    var queryData = {};
    queryData['reach'] = $("#marketInputPublic", parent.document).val();
    queryData["outpatientName"] = multipleSelection($('#outpatientName').val());
    queryData['accessDisease'] = $("#accessDisease").val();  //访问病种
    queryData['appointmentPayService'] = $("#appointmentPayService").val();  //预约病种
    queryData['startFilingTime'] = $("#startFilingTime").val();  //建档时间
    queryData['endFilingTime'] = $("#endFilingTime").val();
    queryData['startRevisitTime'] = $("#startRevisitTime").val();  //回访时间
    queryData['endRevisitTime'] = $("#endRevisitTime").val();
    queryData['revisit'] = $("#follower").val();     //回访人
    queryData['area'] = multipleSelection($('#area').val());
    var param = parseParam(queryData);
    location.href = Feng.ctxPath + '/returnVisitStatistics/returnVisitStatisticsExport?' + param;
};

RevisitStatisticsTable.resetSearch = function () { // 重置
    $("#area").selectpicker('deselectAll');
    $("#outpatientName").selectpicker('deselectAll');
    $("#accessDisease").val("");
    $("#marketInputPublic", parent.document).val("");
    $("#appointmentPayService").val("");
    $("#follower").val("");
    $("#startFilingTime").val("");
    $("#endFilingTime").val("");
    $("#startRevisitTime").val("");
    $("#endRevisitTime").val("");
    RevisitStatisticsTable.search();
};

RevisitStatisticsTable.today = function () { // 点击上面状态筛选表格
    $("#startRevisitTime").val(setDateTime(new Date(),0) + " " + "00:00:00");
    $("#endRevisitTime").val(setDateTime(new Date(),0) + " " + "23:59:59");
    RevisitStatisticsTable.search();
};

RevisitStatisticsTable.yesterday = function () { // 昨天
    $("#startRevisitTime").val(setDateTime(new Date(),1) + " " + "00:00:00");
    $("#endRevisitTime").val(setDateTime(new Date(),1) + " " + "23:59:59");
    RevisitStatisticsTable.search();
};

RevisitStatisticsTable.thisWeek = function () { // 本周
    var date = new Date();
    var currentWeek = date.getDay();
    var stepSunDay = -currentWeek + 1;
    if (currentWeek == 0) {stepSunDay = -7;}
    var stepMonday = 7 - currentWeek;
    var time = date.getTime();
    var monday = new Date(time + stepSunDay * 24 * 3600 * 1000);
    var sunday = new Date(time + stepMonday * 24 * 3600 * 1000);
    $("#startRevisitTime").val(setDateTime(monday,0) + " " + "00:00:00");
    $("#endRevisitTime").val(setDateTime(sunday,0) + " " + "23:59:59");
    RevisitStatisticsTable.search();
};

RevisitStatisticsTable.thisMonth = function () { // 本月
    $("#startRevisitTime").val(firstMonthTime(new Date(),0) + " " + "00:00:00");
    $("#endRevisitTime").val(setMonthTime(new Date(),0) + " " + "23:59:59");
    RevisitStatisticsTable.search();
};

RevisitStatisticsTable.lastMonth = function () { // 上个月
    $("#startRevisitTime").val(firstMonthTime(new Date(),1) + " " + "00:00:00");
    $("#endRevisitTime").val(setMonthTime(new Date(),1) + " " + "23:59:59");
    RevisitStatisticsTable.search();
};

$("#marketSearchAll", parent.document).click(function(){RevisitStatisticsTable.search();});//主页搜索

function phonePrivileges(){   //隐藏手机号
    var phonePrivileges=parent.$("#phonePrivileges").val();
    if(phonePrivileges == 2){
        $('#revisitStatisticsTable').bootstrapTable('hideColumn', 'customerPhone');
    }
}
$(function () {
    var defaultColunms = RevisitStatisticsTable.initColumn();
    var table = new BSTable(RevisitStatisticsTable.id, "/returnVisitStatistics/list", defaultColunms);
    table.setPaginationType("server");
    table.setQueryParams(RevisitStatisticsTable.formParams());
    RevisitStatisticsTable.table = table.init();
    RevisitStatisticsTable.today();
    menuNameHideColumn($("#menuName").val(),'revisitStatisticsTable');
    $(document).on('click','#laydate_clear,#laydate_today,#laydate_ok',function () {RevisitStatisticsTable.getData()});
    checkDate = function () {RevisitStatisticsTable.search()};
    callbackTable = function () {RevisitStatisticsTable.search()};
    phonePrivileges();
});